package home

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/webserver/controllers/util"
	"github.com/gin-gonic/gin"
)

func Home(c *gin.Context) {
	_, err := util.AdminFilter(c)
	if err != nil {
		return
	}

	var machineRunningCount, machineStoppedCount int
	if err := db.DB.Model(&models.Machine{}).
		Where("`status` = ?", models.MachineStatusRunning).
		Count(&machineRunningCount).Error; err != nil {
		util.FailedResponse(c, 10001, "查询失败")
		return
	}
	if err := db.DB.Model(&models.Machine{}).
		Where("`status` = ?", models.MachineStatusStopped).
		Count(&machineStoppedCount).Error; err != nil {
		util.FailedResponse(c, 10002, "查询失败")
		return
	}

	var workerRunningCount, workerStoppedCount int
	if err := db.DB.Model(&models.Worker{}).
		Where("`status` = ?", models.WorkerRunning).
		Count(&workerRunningCount).Error; err != nil {
		util.FailedResponse(c, 10003, "查询失败")
		return
	}
	if err := db.DB.Model(&models.Worker{}).
		Where("`status` = ?", models.WorkerPause).
		Count(&workerStoppedCount).Error; err != nil {
		util.FailedResponse(c, 10004, "查询失败")
		return
	}

	c.JSON(200, gin.H{
		"status":                10000,
		"message":               "success",
		"machine_all_count":     machineRunningCount + machineStoppedCount,
		"machine_running_count": machineRunningCount,
		"machine_stopped_count": machineStoppedCount,
		"worker_all_count":      workerRunningCount + workerStoppedCount,
		"worker_running_count":  workerRunningCount,
		"worker_stopped_count":  workerStoppedCount,
	})

}
